/**
 * @author kzm
 * @version 1.0
 * @license MIT-License (2007)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 
 */
package com.mediacatalyst.net {
	import com.mediacatalyst.lang.ICompareable;
	import com.mediacatalyst.lang.IDeconstructable;
	import com.mediacatalyst.lang.INaturalObject;
	import com.mediacatalyst.lang.deconstructObject;
	import com.mediacatalyst.patterns.prototype.IPrototype;
	import com.mediacatalyst.utils.ILinkedList;
	import com.mediacatalyst.utils.ILinkedNode;	
/**
 * Manipulates a linked loader chain.
 */
	public class LinkedLoaderList implements ILinkedList,INaturalObject,IDeconstructable{
		private var _head:LinkedLoader;
		
		public function LinkedLoaderList(){
			super();
		}
		/**
		 * @see com.mediacatalyst.lang.IDeconstructable#deconstruct()
		 */
		public function deconstruct():void{
			deconstructObject(_head);
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedList#insert()
		 */
		public function insert(item:ILinkedNode):void{
			if(_head==null)
				_head = LinkedLoader(item);
			else{
				var current:LinkedLoader = _head;
				while(current.hasNext())
					current = LinkedLoader(current.next);
				current.next = item;
			}
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedList#remove()
		 */
		public function remove():void{
			if(_head==null)
				return;
			var current:LinkedLoader = _head;
			var last:LinkedLoader;
			while(current.hasNext()){
				last = current;
				current = LinkedLoader(current.next);
			}
			last.next = null;
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedList#insertHead()
		 */
		public function insertHead(item:ILinkedNode):void{
			LinkedLoader(item).next = _head;
			_head = LinkedLoader(item);
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedList#removeHead()
		 */
		public function removeHead():void{
			if(_head==null)
				return;
			if(_head.hasNext())
				_head = LinkedLoader(_head.next);
			else
				_head = null;
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedList#removeNode()
		 */
		public function removeNode(item:ILinkedNode):void{
			if(_head==null)
				return;
			var current:LinkedLoader = _head;
			var last:LinkedLoader;
			while(current.hasNext()){
				if(current === item){
					if(last==null)
						_head = LinkedLoader(current.next);
					else{
						last.next = LinkedLoader(current.next);
						return;
					}
				}
				last = current;
				current = LinkedLoader(current.next);
			}
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedList#insertAt()
		 */
		public function insertAt(pos:uint,item:ILinkedNode):void{
			if(_head==null)
				return;
			if(pos == length){
				insert(item);
				return;
			}
			var i:uint = 0;
			var current:LinkedLoader = _head;
			var last:LinkedLoader;
			while(current != null){
				if(i++==pos){
					if(last==null)
						insertHead(item);
					else{
						last.next = item;
						LinkedLoader(item).next = LinkedLoader(current);
					}
					return;
				}else if(current.hasNext()){
					last = current;
					current = LinkedLoader(current.next);
				}else
					current = null;
			}		
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedList#length
		 */
		public function get length():uint{
			if(_head==null)
				return 0;
			var i:uint = 1;
			var current:LinkedLoader = _head;
			while(current.hasNext()){
				current = LinkedLoader(current.next);
				i++;
			}
			return i;
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedList#removeAt()
		 */
		public function removeAt(pos:uint):void{
			if(_head==null)
				return;
			var i:uint = 0;
			var current:LinkedLoader = _head;
			var last:LinkedLoader;
			while(current != null){
				if(i++==pos){
					if(last==null)
						removeHead();
					else
						last.next = LinkedLoader(current.next);
					return;
				}else if(current.hasNext()){
					last = current;
					current = LinkedLoader(current.next);
				}else
					current = null;
			}		
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedList#head
		 */
		public function get head():ILinkedNode{
			return _head;
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedList#tail
		 */
		public function get tail():ILinkedNode{
			if(_head==null)
				return null;
			var current:LinkedLoader = _head;
			while(current.hasNext())
				current = LinkedLoader(current.next);
			return current;
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedList#at()
		 */
		public function at(pos:uint):ILinkedNode{
			if(_head==null)
				return null;
			var i:uint = 0;
			var current:LinkedLoader = _head;
			while(current != null){
				if(i++==pos){
					return current;
				}else if(current.hasNext())
					current = LinkedLoader(current.next);
				else
					current = null;
			}
			return null;
		}
		/**
		 * @see com.mediacatalyst.utils.ILinkedList#empty()
		 */
		public function empty():Boolean{
			return _head==null;
		}
		/**
		 * @see com.mediacatalyst.lang.IPrintable#toString()
		 */
		public function toString():String{
			return "LinkedLoaderList(list="+(head!=null?LinkedLoader(head).toString():"null")+")";
		}
		/**
		 * @see com.mediacatalyst.lang.ICompareable#equals()
		 */
		public function equals(other:ICompareable):Boolean{
			return (other is LinkedLoaderList) && 
			LinkedLoader(head).equals(LinkedLoader(LinkedLoaderList(other).head));
		}
		/**
		 * @see com.mediacatalyst.patterns.prototype.IPrototype#clone()
		 */
		public function clone():IPrototype{
			var ll:LinkedLoaderList = new LinkedLoaderList();
			ll.insert(LinkedLoader(INaturalObject(head).clone()));
			return ll;
		}
	}
}